package com.ybkj.daijia.server.controller;

/**
 * @author zhyu
 * @date
 */

import com.ybkj.daijia.serverUtils.ExcelUtil;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.Log4JLog;
import com.ybkj.daijia.server.service.Log4JLogService;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "log4JLog")
public class Log4JLogController {

    @Autowired
    private Log4JLogService Log4JLogService;


    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(
        @RequestParam(required = false, value = "start") Date start,
        @RequestParam(required = false, value = "end") Date end,
        @RequestParam(required = false, value = "location") String location,
        @RequestParam(required = false, value = "thread") String thread,
        @RequestParam(required = false, value = "level") String level,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "size", defaultValue = "20") Integer size,
        @RequestParam(required = false, value = "pager.offset") Integer offset
    ) throws ParseException {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("Log4JLog")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("log4JLog/list");
            if (null != offset && offset > -1)
            //page 是从0开始算的
            {
                page = offset / 20;
            }
            Map<String, Object> map = boxParamIntoMap(thread, location, level, start, end, page,
                size);
            mv.addObject("page", Log4JLogService.selectByMap(map));
            mv.addObject("start", start);
            mv.addObject("end", end);
            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> delete(Long id, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("Log4JLog")) {
            Result<Integer> result = new Result<Integer>();


        } else {
            throw new NoPermissionException();
        }
        return null;

    }

    private Map<String, Object> boxParamIntoMap(String thread, String location, String level,
        Date start, Date end, Integer page, Integer size) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(thread)) {
            map.put("thread", thread.trim());
        }
        if (StringUtils.isNotBlank(location)) {
            map.put("location", location.trim());
        }
        if (StringUtils.isNotBlank(level)) {
            map.put("level", level.trim().toUpperCase());
        }
        if (null != start) {
            map.put("start", start);
        }
        if (null != end) {
            map.put("end", end);
        }
        if (null != page && null != size && -1 != size) {
            map.put("page", page);
        } else {
            map.put("page", 0);
        }
        if (null != size && -1 != size) {
            map.put("size", size);
        } else {
            map.put("size", 20);
        }
        return map;
    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    public @ResponseBody
    String export(
        @RequestParam(required = false, value = "start") Date start,
        @RequestParam(required = false, value = "end") Date end,
        @RequestParam(required = false, value = "location") String location,
        @RequestParam(required = false, value = "thread") String thread,
        @RequestParam(required = false, value = "level") String level,
        @RequestParam(required = true, value = "timestamp") Long timestamp,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "size") Integer size,
        HttpServletRequest req, HttpServletResponse response,
        HttpSession session) throws IOException, ParseException {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("Log4JLog")) {
            Map<String, Object> map = boxParamIntoMap(thread, location, level, start, end, page,
                size);
            List<Log4JLog> content = Log4JLogService.export(session, map, timestamp);

            List<List<String>> rowlist = new ArrayList<List<String>>();
            String[] head = new String[27];
            head[0] = ("线程");
            head[1] = ("详情");
            head[2] = ("时间");
            head[3] = ("地址");
            head[4] = ("日志等级");

            SimpleDateFormat format = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
            for (Log4JLog log4JLog : content) {
                List<String> celllist = new ArrayList<String>();

                celllist.add(log4JLog.getThread());
                celllist.add(log4JLog.getInfo());
                celllist.add(format.format(log4JLog.getHappen()));
                celllist.add(log4JLog.getLocation());
                celllist.add(log4JLog.getLevel());
                rowlist.add(celllist);
            }

            return ExcelUtil.createExcel(req, response, head, rowlist);
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "exportProcess", method = RequestMethod.GET)
    public @ResponseBody
    Object exportProcess(HttpSession session,
        @RequestParam(required = true, value = "timestamp") Long timestamp) {
        return session.getAttribute("exportLog4JLogProcess_" + timestamp);
    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}

